package cn.book.action;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import cn.book.entity.Book;
import cn.book.serves.BookServe;

public class BookAction extends BaseAction{
	private File picture;
	private String pictureContentType;
	private String pictureFileName;
	private String savePath;
	private Book book;
	public File getPicture() {
		return picture;
	}
	public void setPicture(File picture) {
		this.picture = picture;
	}
	public String getPictureContentType() {
		return pictureContentType;
	}
	public void setPictureContentType(String pictureContentType) {
		this.pictureContentType = pictureContentType;
	}
	public String getPictureFileName() {
		return pictureFileName;
	}
	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	//图书添加 增加上传文件的处理
	public String addBook() throws Exception{
		BookServe bookServe=new BookServe();
		String path=ServletActionContext.getServletContext().getRealPath("/bookpicture");
		// 创建目标文件对象
		File destFist=new File(path,pictureFileName);
		// 把上传的文件，拷贝到目标文件中
		FileUtils.copyFile(picture, destFist);
		boolean flag=bookServe.addBook(book.getBookname(), book.getAuthor(), book.getPublish(), book.getPrice(), book.getCurrcount(), pictureFileName);
		if (flag) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	//查询所有的图书信息
	public String queryBookList(){
		BookServe bookServe=new BookServe();
		List<Book> list=bookServe.queryBookList();
		if (list!=null) {
			getMapSession().put("list", list);
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	//根据出版社查询图书信息
	public String selectPublish(){
		BookServe bookServe=new BookServe();
		List<Book> list=bookServe.selectPublish(book.getPublish());
		if (list!=null) {
			getMapSession().put("list", list);
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	//根据当前库存插叙图书信息
	public String selectCurrcount(){
		BookServe bookServe=new BookServe();
		List<Book> list=bookServe.selectCurrcount(book.getCurrcount());
		if (list!=null) {
			getMapSession().put("list", list);
			return SUCCESS;
		}else {
			return ERROR;
		}
	}
	//删除
	public String deleteBook(){
		BookServe bookServe=new BookServe();
		boolean flag=bookServe.deleteBook(book.getBookid());
		if (flag) {
			return SUCCESS;
		}else {
			return ERROR;
		}
	}
	

}
